﻿using System;
using System.Collections.Generic;
using System.Text;
using org.smslib;
using DatabaseProvider;
using System.Windows.Forms;
using System.IO;
using LogHelper;
using System.Drawing;
namespace sms_modem_control
{
    public class InboundNotification : IInboundMessageNotification
    {
        public void process(AGateway gateway, org.smslib.Message.MessageTypes msgType, InboundMessage msg)
        {
          if (msgType == org.smslib.Message.MessageTypes.INBOUND) {
               // Programe.control.upDateMonitorBack(">>> New Inbound message detected from Gateway: " + gateway.getGatewayId())
                //Update to database
                try
                {
                    long iStart = DateTime.Now.Ticks * 100 / 1000000;
                    Programe.control.upDateMonitorBack("Receive new messge from: " + msg.getOriginator() + " content: " + msg.getText() + ". Begin insert into database...", Color.BlueViolet);

                    //Begin insert into database
                    int iRecord = MSSQL.ExecuteCommandNoneQuery("insert into tbl_sms_request( date_time_recieve,  process_status, sms_content, isdn_sender) VALUES (getDate(),'0','" + msg.getText() + "','" + msg.getOriginator() + "')");

                    long iTotal = DateTime.Now.Ticks * 100 / 1000000 - iStart;
                    Programe.control.upDateMonitorBack("Insert " + iRecord + " record(s) successfull into database. Total time: " + iTotal +" ms", Color.BlueViolet);
                }
                catch (Exception ex) 
                {
                    Programe.control.upDateMonitorBack("Receive from " + msg.getOriginator() + " is error: " + ex.Message, Color.Red);
                    Log.logTransaction(ex);
                }
            }
            //           else if (msgType == org.smslib.Message.MessageTypes.STATUSREPORT) Programe.control.upDateMonitorBack(">>> New Inbound Status Report message detected from Gateway: " + gateway.getGatewayId());
           // Programe.control.upDateMonitorBack(msg.toString());
            try
            {
                // Uncomment following line if you wish to delete the message upon arrival.
                gateway.deleteMessage(msg);
            }
            catch (Exception e)
            {
                Programe.control.upDateMonitorBack(e.Message, Color.Red);
                Programe.control.upDateMonitorBack(e.StackTrace, Color.Red);
                Log.logTransaction(e);
            }
        }
    }
}
